<template>
  <el-card shadow="hover" class="border-0">
    <list-header @refresh="getData" @create="handleCreate" />

    <el-tree
      :data="tableData"
      :props="{ children: 'child', label: 'name' }"
      v-loading="loading"
      node-key="id"
    >
      <template #default="{ node, data }">
        <div class="custom-tree-node">
          <span>{{ data.name }}</span>

          <div class="ml-auto" @click.stop="() => {}">
            <el-button type="primary" size="small" @click.stop="openGoodsDrawer(data)" text :loading="data.goodsDrawerLoading">
              推荐商品
            </el-button>
            <el-switch
              :modelValue="data.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange($event, data)"
            />

            <el-button
              type="primary"
              size="small"
              @click.stop="handleEdit(data)"
              text
              class="mr-[12px]"
            >
              修改
            </el-button>
           
            <span @click.stop="() => {}">
              <el-popconfirm
                title="是否要删除该记录？ "
                confirmButtonText="确认"
                cancelButtonText="取消"
                @confirm="handleDelete(data.id)"
              >
                <template #reference>
                  <el-button text type="primary" size="small"> 删除 </el-button>
                </template>
              </el-popconfirm>
            </span>
          </div>
        </div>
      </template>
    </el-tree>

    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSumbit" @cancel="handleCancel">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
      </el-form>
    </FormDrawer>

    <GoodsDrawer ref="goodsDrawerRef"/>
  </el-card>
</template>

<script setup>
  import { ref } from 'vue'
  import ListHeader from '@/components/ListHeader.vue'
  import FormDrawer from '@/components/FormDrawer.vue'
  import { useInitTable, useInitForm } from '@/hooks/useCommon.js'
  import GoodsDrawer from './components/GoodsDrawer.vue'

  import {
    getCategoryListApi,
    createCategoryApi,
    updateCategoryApi,
    updateCategoryStatusApi,
    deleteCategoryApi
  } from '@/api/category.js'

  const goodsDrawerRef = ref(null)

  const {
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange,
  } = useInitTable({
    getlist: getCategoryListApi,
    delete: deleteCategoryApi,
    updateStatus: updateCategoryStatusApi,
    onGetListSuccess: (res) => {
      tableData.value = res.map((o) => {
        o.goodsDrawerLoading = false
        return o
      })
    },
  })

  const {
    formRef,
    formDrawerRef,
    form,
    rules,
    drawerTitle,
    editId,
    handleCreate,
    handleEdit,
    handleSumbit,
    handleCancel,
  } = useInitForm({
    form: {
      name: '',
    },
    rules: {},
    getData,
    create: createCategoryApi,
    update: updateCategoryApi,
  })

  function openGoodsDrawer(data) {
    goodsDrawerRef.value.open(data)
  }

</script>

<style scoped>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-right: 8px;
  }
  :deep(.el-tree-node__content) {
    padding: 20px 0 !important;
  }
  /* tree 子元素 */
  :deep(.el-tree-node__children) {
    padding-left: 20px;
  }
  .el-tag {
    margin-right: 5px;
  }
</style>
